Change the way that networks and PIFs relate to each other -- the PIF holds a
authorEwan Mellor <ewan@xensource.com>
Mon, 25 Dec 2006 16:26:51 +0000 (16:26 +0000)
committerEwan Mellor <ewan@xensource.com>
Mon, 25 Dec 2006 16:26:51 +0000 (16:26 +0000)
reference to the network (and the host) not the other way around.  Persist only
the necessary details for the network (not the set of PIFs and VIFs).

Fix the plumbing of the network class inside XendAPI, and implement a few more
functions.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
tools/python/xen/xend/XendAPI.py
tools/python/xen/xend/XendNetwork.py
tools/python/xen/xend/XendNode.py
tools/python/xen/xend/XendPIF.py

index 2bff4913e2527540bec239fef2e3437b67ed69a0..e5ba2f898b2b2902e3ad0fde43fea4f0a6fbf05f 100644 (file)
@@ -451,15 +451,31 @@ class XendAPI:
         return xen_api_error(XEND_ERROR_UNSUPPORTED)
 
 
-    # Xen API: Class Network
+    # Xen API: Class network
     # ----------------------------------------------------------------
 
-    Network_attr_ro = ['VIFs', 'PIFs']
-    Network_attr_rw = ['name_label',
+    network_attr_ro = ['VIFs', 'PIFs']
+    network_attr_rw = ['name_label',
                        'name_description',
                        'default_gateway',
                        'default_netmask']
 
+    def _get_network(self, ref):
+        return XendNode.instance().get_network(ref)
+
+    def network_get_all(self, session):
+        return xen_api_success(XendNode.instance().get_network_refs())
+
+    def network_get_record(self, session, ref):
+        return xen_api_success(
+            XendNode.instance().get_network(ref).get_record())
+
+    def network_get_VIFs(self, _, ref):
+        return xen_api_success(self._get_network(ref).get_VIF_UUIDs())
+
+    def network_get_PIFs(self, session, ref):
+        return xen_api_success(self._get_network(ref).get_PIF_UUIDs())
+
     # Xen API: Class PIF
     # ----------------------------------------------------------------
 
index 120dc5f03f9cf5f36fcebe227d95dd545030a76f..7db08805d9b3aeb751edf5113d6b57ff4785898f 100644 (file)
@@ -12,7 +12,6 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #============================================================================
-# Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
 # Copyright (c) 2006 Xensource Inc.
 #============================================================================
 
@@ -22,7 +21,8 @@ import re
 import struct
 import socket
 
-from xen.xend.XendRoot import instance as xendroot
+import XendNode
+from XendLogging import log
 
 IP_ROUTE_RE = r'^default via ([\d\.]+) dev (\w+)'
 
@@ -63,57 +63,37 @@ class XendNetwork:
         self.name_description = description
         self.default_gateway = gateway
         self.default_netmask = netmask
-        self.vifs = {}
-        self.pifs = {}
-
-    def get_name_label(self):
-        return self.name_label
 
     def set_name_label(self, new_name):
         self.name_label = new_name
 
-    def get_name_description(self):
-        return self.name_description
-
     def set_name_description(self, new_desc):
         self.name_description = new_desc
 
-    def get_default_gateway(self):
-        return self.default_gateway
-
     def set_default_gateway(self, new_gateway):
         if re.search('^\d+\.\d+\.\d+\.\d+$', new_gateway):
             self.default_gateway = new_gateway
 
-    def get_default_netmask(self):
-        return self.default_netmask
-
     def set_default_netmask(self, new_netmask):
         if re.search('^\d+\.\d+\.\d+\.\d+$', new_netmask):
             self.default_netmask = new_netmask
 
-    def add_pif(self, pif):
-        self.pifs[pif.get_uuid()] = pif
-
-    def remove_pif(self, pif_uuid):
-        if pif_uuid in self.pifs:
-            del self.pifs[pif_uuid]
+    def get_VIF_UUIDs(self):
+        return []
 
-    def add_vif(self, vif):
-        self.vifs[vif.get_uuid()] = vif
+    def get_PIF_UUIDs(self):
+        return [x.uuid for x in XendNode.instance().pifs.values()
+                if x.network == self]
 
-    def remove_vif(self, vif_uuid):
-        if vif_uuid in self.vifs:
-            del self.vifs[vif_uuid]
-        
-    def get_record(self):
-        return {
+    def get_record(self, transient = True):
+        result = {
             'uuid': self.uuid,
             'name_label': self.name_label,
             'name_description': self.name_description,
             'default_gateway': self.default_gateway,
             'default_netmask': self.default_netmask,
-            'VIFs': self.vifs.keys(),
-            'PIFs': self.pifs.keys()
         }
-           
+        if transient:
+            result['VIFs'] = self.get_VIF_UUIDs()
+            result['PIFs'] = self.get_PIF_UUIDs()
+        return result
index 401e0b688147a674cf964b46613164e4d6836cdc..3e3ae93b7b122c6874672a6c70c4cbec2ebd2b7f 100644 (file)
@@ -83,20 +83,6 @@ class XendNode:
         self.pifs = {}
         self.networks = {}
 
-        # initialise PIFs
-        saved_pifs = self.state_store.load_state('pif')
-        if saved_pifs:
-            for pif_uuid, pif in saved_pifs.items():
-                self.pifs[pif_uuid] = XendPIF(pif_uuid,
-                                              pif['name'],
-                                              pif['MTU'],
-                                              pif['MAC'])
-        else:
-            for name, mtu, mac in linux_get_phy_ifaces():
-                pif_uuid = uuid.createString()
-                pif = XendPIF(pif_uuid, name, mtu, mac)
-                self.pifs[pif_uuid] = pif
-
         # initialise networks
         saved_networks = self.state_store.load_state('network')
         if saved_networks:
@@ -106,17 +92,31 @@ class XendNode:
                                 network.get('name_description', ''),
                                 network.get('default_gateway', ''),
                                 network.get('default_netmask', ''))
-                
-                for pif_uuid in network.get('PIFs', {}).keys():
-                    pif = self.pifs.get(pif_uuid)
-                    if pif:
-                        self.networks.pifs[pif_uuid] = pif
         else:
             gateway, netmask = linux_get_default_network()
             net_uuid = uuid.createString()
             net = XendNetwork(net_uuid, 'net0', '', gateway, netmask)
             self.networks[net_uuid] = net
 
+        # initialise PIFs
+        saved_pifs = self.state_store.load_state('pif')
+        if saved_pifs:
+            for pif_uuid, pif in saved_pifs.items():
+                if pif['network'] in self.networks:
+                    network = self.networks[pif['network']]
+                    self.pifs[pif_uuid] = XendPIF(pif_uuid,
+                                                  pif['name'],
+                                                  pif['MTU'],
+                                                  pif['MAC'],
+                                                  network,
+                                                  self)
+        else:
+            for name, mtu, mac in linux_get_phy_ifaces():
+                network = self.networks.values()[0]
+                pif_uuid = uuid.createString()
+                pif = XendPIF(pif_uuid, name, mtu, mac, network, self)
+                self.pifs[pif_uuid] = pif
+
         # initialise storage
         saved_sr = self.state_store.load_state('sr')
         if saved_sr and len(saved_sr) == 1:
@@ -125,7 +125,6 @@ class XendNode:
         else:
             sr_uuid = uuid.createString()
             self.sr = XendStorageRepository(sr_uuid)
-        self.save()
 
     def save(self):
         # save state
@@ -133,10 +132,10 @@ class XendNode:
                                    'name_description':self.desc}}
         self.state_store.save_state('host',host_record)
         self.state_store.save_state('cpu', self.cpus)
-        pif_records = dict([(k, v.get_record())
+        pif_records = dict([(k, v.get_record(transient = False))
                             for k, v in self.pifs.items()])
         self.state_store.save_state('pif', pif_records)
-        net_records = dict([(k, v.get_record())
+        net_records = dict([(k, v.get_record(transient = False))
                             for k, v in self.networks.items()])
         self.state_store.save_state('network', net_records)
 
@@ -164,6 +163,9 @@ class XendNode:
     def is_valid_cpu(self, cpu_ref):
         return (cpu_ref in self.cpus)
 
+    def is_valid_network(self, network_ref):
+        return (network_ref in self.networks)
+
     #
     # Storage Repo
     #
@@ -233,7 +235,17 @@ class XendNode:
     def get_host_cpu_load(self, host_cpu_ref):
         return 0.0
 
+
+    #
+    # Network Functions
+    #
     
+    def get_network_refs(self):
+        return self.networks.keys()
+
+    def get_network(self, network_ref):
+        return self.networks[network_ref]
+
 
     #
     # Getting host information.
@@ -318,5 +330,5 @@ def instance():
         inst
     except:
         inst = XendNode()
+        inst.save()
     return inst
-
index ebf9605e8f30abe1af10dc0dd5370cceaef177d5..32c4b3626b291ec70901e819f94769a3f14a2b74 100644 (file)
@@ -12,7 +12,6 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #============================================================================
-# Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
 # Copyright (c) 2006 Xensource Inc.
 #============================================================================
 
@@ -100,29 +99,18 @@ def same_dir_rename(old_path, new_path):
 class XendPIF:
     """Representation of a Physical Network Interface."""
     
-    def __init__(self, uuid, name, mtu, mac):
+    def __init__(self, uuid, name, mtu, mac, network, host):
         self.uuid = uuid
         self.name = name
         self.mac = mac
         self.mtu = mtu
         self.vlan = ''
-        self.network = None
+        self.network = network
+        self.host = host
 
     def set_name(self, new_name):
         self.name = new_name
             
-    def get_name(self):
-        return self.name
-
-    def get_uuid(self):
-        return self.uuid
-
-    def get_mac(self):
-        return self.mac
-
-    def get_vlan(self):
-        return self.vlan
-
     def set_mac(self, new_mac):
         success = linux_set_mac(new_mac)
         if success:
@@ -141,17 +129,14 @@ class XendPIF:
     def get_io_write_kbs(self):
         return 0.0
 
-    def get_record(self):
-        if self.network:
-            network_uuid = self.network.get_uuid()
-        else:
-            network_uuid = None
-
-        return {'name': self.name,
-                'MAC': self.mac,
-                'MTU': self.mtu,
-                'VLAN': self.vlan,
-                'network': network_uuid,
-                'io_read_kbs': self.get_io_read_kbs(),
-                'io_write_kbs': self.get_io_write_kbs()}
-                
+    def get_record(self, transient = True):
+        result = {'name': self.name,
+                  'MAC': self.mac,
+                  'MTU': self.mtu,
+                  'VLAN': self.vlan,
+                  'host': self.host.uuid,
+                  'network': self.network.uuid}
+        if transient:
+            result['io_read_kbs'] = self.get_io_read_kbs()
+            result['io_write_kbs'] = self.get_io_write_kbs()
+        return result